package com.fresh.liangyi.dao;

import com.fresh.liangyi.model.domain.Address;
import com.fresh.liangyi.model.domain.Goods;
import com.fresh.liangyi.model.domain.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface OrderMapper {
    @Insert("INSERT INTO order_detail(goods_id,order_id) values(#{gId},#{aId})")
    public Integer orderDetailsAdd(Integer gId,Integer aId);

    @Insert("INSERT INTO `order`(user_id,status,total_amount,address_id,create_date) " +
            " VALUES (#{userId},#{status}, #{totalAmount}, #{addressId}, #{createDate})")
    @Options(useGeneratedKeys = true,keyProperty="id",keyColumn = "id")//返回主键到实体类中
    public Integer orderAdd(Order order);

    //根据订单号显示所有商品信息
    @Select("SELECT * FROM goods INNER JOIN  `order_detail` on goods.id=order_detail.goods_id and order_detail.order_id=#{orderId} ORDER BY goods.sales_volume DESC")
    public List<Goods> showGoodsByOrderId(Integer orderId);

    //根据用户id显示所有订单信息
    @Select("SELECT * FROM `order` WHERE user_id=#{id} ORDER BY create_date DESC")
    public List<Order> showOrderByUserId(Integer userId);

    //显示所有订单信息
    @Select("SELECT * FROM `order` ORDER BY create_date DESC")
    public List<Order> showOrder();


    //根据订单id显示订单信息
    @Select("SELECT * FROM `order` WHERE id=#{id}")
    public Order selectOrderById(Integer orderId);

    //根据订单显示地址
    @Select("SELECT * FROM address  JOIN `order` ON address.id=`order`.address_id AND `order`.id=#{id}")
    public Address getAddressByOrderId(Integer orderId);

    public Integer updateOrder(Order order);
}
